<script type="text/javascript">
var enable_submit=true;
function submit_volume_update(){ 
    var modal_selector = $("#volume_update_modal");
    var snappolicy_dropdown = modal_selector.find('#snappolicy_dropdown');
    var bValid = true;
    var qos = modal_selector.find("#volume_update_qos_dropdown");
    var access_policy = modal_selector.find("#volume__update_isolation_dropdown");
    var repnum = modal_selector.find("#repnum");
    var target_tier = modal_selector.find("#volume_tt_select");
    var chap_name = modal_selector.find("#chap_name");
    var chap_password = modal_selector.find("#chap_password");
    var capacity = modal_selector.find("#capacity_cr");
    var analysis_latency = modal_selector.find(".analysis_latency:checked").val();
    bValid = bValid && checkNull(capacity, "存储容量不允许为空")
    bValid = bValid && checkRegexp_num(capacity, /^[0-9]+$/i, "存储容量必须为大于0的整数");
    bValid = bValid && checkCapacity(capacity, 1, 8000, "存储容量应大于1G，小于等于8000G");
    if($.trim(chap_name.val()).length>0||$.trim(chap_password.val()).length>0)   {
        bValid = bValid && checkCHAP(chap_name, chap_password);
    }

    if(bValid){
        if(enable_submit){ 
            enable_submit=false;
            var $submitBtn = $('.modal-footer').find('.btn-blue');
            $submitBtn.html('<i class="fa fa-spinner fa-spin" style="margin:3px 6px;"></i>');
            $('.rtn-tip').slideUp('fast');
            $.post("/volume/update", {
                "snappolicy_id":snappolicy_dropdown.attr("realvalue"),
                "size_gb" : capacity.val(),
                "qos_id" : qos.attr('realvalue'),
                "repnum":$.trim(repnum.attr("realvalue")),
                "priority":$.trim(target_tier.attr("realvalue")),
                "access_policy_id": access_policy.attr('realvalue'),
                'chap_name': $.trim(chap_name.val()),
                'chap_password': $.trim(chap_password.val()),
                'analysis_latency': analysis_latency,
                "id": "{{volume.id}}"
            },
            function(data){
                if (data.reply.is_success){
                    angular.element('#volume_controller').scope().close();
                    $('#volume_available_tb').trigger("reloadGrid")
                    enable_submit=true;
                }else{
                    error =  data.reply.error;
                    $('.rtn-tip').text(error).slideDown('fast');
                    $submitBtn.html(SelfTranslate("[[ 'label ok'|translate ]]"));
                    enable_submit=true;
                }
            });
        }
    }
}

$(function(){

    if ('{{pool.protocol}}' == 'nbd') {
        $('.user-info').hide();
    }

    var volume_isolation = [['','默认允许全部']];
    {% for isolation in isolations %}
        var values = new Array();
        values[0] = "{{isolation.id}}";
        values[1] = "{{isolation.name}}";
        volume_isolation.push(values);
    {% endfor %}
    var volume_qos = [['','默认无限制']];
    {% for qos in qoses %}
        var values = new Array();
        values[0] = "{{qos.id}}";
        values[1] = "{{qos.name}}";
        volume_qos.push(values);
    {% endfor %}
    var snappolicies = [['', '默认无自动快照']];
    {% for p in snappolicy %}
        var values = new Array();
        values[0] = "{{ p.id }}";
        values[1] = "{{ p.name }}";
        snappolicies.push(values)
    {% endfor %}

    $('#volume__update_isolation_dropdown').rqDropDown({
        position:true,
        "datatype":"local",
        "source":volume_isolation,	
    });
    $("#volume_update_qos_dropdown").rqDropDown({
        position:true,
        "datatype":"local",
        "source":volume_qos,    
    });
    $('#snappolicy_dropdown').rqDropDown({
        position:true,
        "datatype":"local",
        "source":snappolicies
    });
    $('#volume_update_modal #repnum').rqDropDown({
        position:true,
        "datatype":"local",
        "source":get_repnums()
    }); 
    $('#volume_update_modal #volume_tt_select').rqDropDown({
        position:true,
        "datatype":"local",
        "source":get_tiers()
    });
    var delete_code = [['2', '2'],['3', '3'],['4', '4'],['6', '6'],['8', '8'],['12', '12'],['16', '16']];
    $('#pool_update_modal #delete_code').rqDropDown({
        "datatype":"local",
        "source":delete_code
    });
    var delete_code_r = [['1', '1'],['2', '2'],['3', '3'],['4', '4']];
    $('#pool_update_modal #delete_code_r').rqDropDown({
        "datatype":"local",
        "source":delete_code_r
    });
    /* 为小图标绑定鼠标移入移出事件 */
    var obj = {
        'capacity': '存储容量必须为大于0的整数。',
        'chap_password': '可为空, 为空时将使用所属资源池的CHAP用户名称;由3至32位小写英文字母、数字、中划线构成。',
        'chap_name': '可为空, 为空时将使用所属资源池的CHAP用户名称;如果资源池未设置CHAP用户，则使用所属用户的CHAP用户;由3至32位小写英文字母、数字、中划线构成'
    }
    bindMouseEvent('#volume_update_modal',obj);
    /* 为小图标绑定鼠标移入移出事件结束 */
});
</script>
<div class="modal-header">  
    <h3>编辑卷</h3>  
</div> 
<div  class="modal-body modal-body-height" id="volume_update_modal">

<div class="volume-update-box">
<div class="rtn-tip"></div>
<table class="base-info">
    <tr>
        <td>
            <label>[['model volume name'|translate]]:</label><a>*</a>
        </td>
        <td colspan="3">
            <label><b>{{volume.name}}</b></label>
        </td>
    </tr>
    <tr>
        <td>
            <label>[['model storage capacity'|translate]]</label><a>*</a>
        </td>
        <td>
            <input id="capacity_cr" name="capacity_cr" size="25" max="10000" min="1" type="text" value="{{volume.size_gb}}"/>
            <span class="unit">G</span>
            <i class="fa fa-question-circle" itype="capacity"></i>
        </td>
        <td>
            <label>[['Disk configuration'|translate]]</label><a>*</a>
        </td>
        <td>
            {% if volume.provisioning == 'thick' %}
           <label style="color: #cccccc;"><input disabled type="radio" name="disk_preparation" value="thin" style="vertical-align:-3px;" />精简 </label>
           <label style="color: #cccccc;"><input disabled type="radio" name="disk_preparation" value="thick" style="vertical-align:-3px;" checked="true"/>非精简 </label>
            {% else %}
           <label style="color: #cccccc;"><input disabled type="radio" name="disk_preparation" value="thin" style="vertical-align:-3px;" checked="true" />精简 </label>
           <label style="color: #cccccc;"><input disabled type="radio" name="disk_preparation" value="thick" style="vertical-align:-3px;" />非精简 </label>
            {% endif %}
        </td>
    </tr>
</table>

<table class="super-info">
    <tr>
        <td>
            <label>保护域</label>
        </td>
        <td>
            <input id="volume_PDs_select" readonly="readonly" class="rounded_dropdown" value='default' realvalue=""/>
            
        </td>
        <td>
            <label>QoS</label>
        </td>
        <td>
            {% if volume.qos_id %}
                    <input id="volume_update_qos_dropdown" readonly="readonly" class="rounded_dropdown" value='{{ volume.qos.name }}' realvalue="{{ volume.qos_id }}"/>
                {% else %}
                    <input id="volume_update_qos_dropdown" readonly="readonly" class="rounded_dropdown" value='默认无限制' realvalue=""/>
                {% endif %}
        </td>
    </tr>
    <tr>
        <td>
            <label>[['replication number'|translate]]</label>
        </td>
        <td>
                        {% if pool.ec_data %}

            <input id="delete_code"  style="width:40%;" readonly="readonly" class="rounded_dropdown" value='{{pool.ec_data}}' realvalue="{{pool.ec_data}}"/>
            <input  id="delete_code_r"  style="width:40%;" readonly="readonly" class="rounded_dropdown" value='{{pool.ec_code}}' realvalue="{{pool.ec_code}}"/>
                            {% else %}
                            <input id="repnum" readonly="readonly" class="rounded_dropdown" value='{{volume.repnum}}' realvalue="{{volume.repnum}}"/>
                        {% endif %}
            </td>
        <td>
            <label>目标分层</label>
        </td>
        <td>
            <input id="volume_tt_select" readonly="readonly" class="rounded_dropdown" value="[['{{volume.priority}}'|translate]]" realvalue='{{volume.priority}}'/>
        </td>
        </tr>

    <tr>
        <td>
            <label>[['modal access strategy'|translate]]</label>
        </td>
        <td>
            {% if volume.access_policy_id %}
                    <input id="volume__update_isolation_dropdown" readonly="readonly" class="rounded_dropdown" value='{{volume.access_policy.name}}' realvalue="{{volume.access_policy_id}}"/>
                {% else %}
                    <input id="volume__update_isolation_dropdown" readonly="readonly" class="rounded_dropdown" value='默认允许全部' realvalue=""/>
                {% endif %}
        </td>
        <td>
            <label>自动快照</label>
        </td>
        <td>
            {% if volume.snappolicies %}
                    <input id="snappolicy_dropdown" readonly="readonly" class="rounded_dropdown"  value='{{volume.snappolicies[0].name}}' realvalue="{{volume.snappolicies[0].id}}"/>
                {% else %}
                    <input id="snappolicy_dropdown" readonly="readonly" class="rounded_dropdown"  value='默认无自动快照' realvalue=""/>
                {% endif %}
        </td>
    </tr>
    </table>
    <table class="switch-info">
    <tr>
        <td>
            <label>Latency监控开关</label>
        </td>
        <td>
            {% if volume.analysis_latency %}
            <label><input class="analysis_latency" type="radio" name="analysis_latency" value="" style="vertical-align:-3px;" />关闭 </label>
            <label><input class="analysis_latency" type="radio" name="analysis_latency" value="1000" style="vertical-align:-3px;" checked="true"/>开启 </label>
            {% else %}
            <label><input class="analysis_latency" type="radio" name="analysis_latency" value="" style="vertical-align:-3px;" checked="true"/>关闭 </label>
            <label><input class="analysis_latency" type="radio" name="analysis_latency" value="1000" style="vertical-align:-3px;"/>开启 </label>
            {% endif %}
        </td>
    </tr>
</table>
<table class="user-info">
    <tr>
        <td>
            <label>[["label chap name"|translate]]</label>
        </td>
        <td>
            <input type="text" name="chap_name" id="chap_name" value="{{ volume.sp_chap_name|default('',True)}}"/>
            <i class="fa fa-question-circle" itype="chap_name"></i>
        </td>
        <td>
            <label>[["label chap password"|translate]]</label>
        </td>
        <td>
            <input type="password" name="chap_password" id="chap_password" value="{{ volume.sp_chap_password|default('',True)}}" readonly onfocus="this.removeAttribute('readonly');" onpaste="return false" oncontextmenu="window.event.returnValue=false" />
            <i class="fa fa-eye" style="right:60px"></i>
            <i class="fa fa-question-circle" itype="chap_password"></i>
        </td>
    </tr>
</table>
<div class="tip">
    <p></p>
    <span></span>
</div>
<div class="error-tip">
    <p></p><span></span>
</div>
</div>

</div>
<div class="modal-footer">  
    <button class="btn btn-blue" ng-click="update()">[[ 'label ok'|translate ]]</button>  
    <button class="btn btn-white" ng-click="cancel()">[[ 'label cancel'|translate ]]</button>  
</div>
